﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class ZiDianBianJi : BaseForm
    {
        public ZiDianBianJi()
        {
            InitializeComponent();
        }

        private DataGridViewRow m_FocusedRow;

        public ZiDianBianJi(DataGridViewRow FocusedRow)
        {
            InitializeComponent();
            m_FocusedRow = FocusedRow;
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_xiangmumingcheng.Text) || string.IsNullOrEmpty(cmb_fenzu.Text))
            {
                KeaoUtils.ShowInfoMessage("信息不完整，请检查后重试。");
                return;
            }

            if (m_FocusedRow == null)
            {
                //新增
                TableRow newRow = new TableRow("tbl_zidian",Guid.NewGuid().ToString(), DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName="fenzu", ValueMember=cmb_fenzu.Text, ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName="xiangmu", ValueMember=txt_xiangmumingcheng.Text, ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName="shijian",ValueMember = DateTime.Now,ValueType = DataValueCommonType.日期时间 });
                newRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="0", ValueType= DataValueCommonType.字符串});
                newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户","站点"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户","客户编号"), ValueType = DataValueCommonType.字符串 });

                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                //修改
                TableRow newRow = new TableRow("tbl_zidian", string.Format("{0}",m_FocusedRow.Cells["col_id"].Value), DBCtl.修改);
                newRow.DataCols.Add(new TableCol() { ColName = "fenzu", ValueMember = cmb_fenzu.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "xiangmu", ValueMember = txt_xiangmumingcheng.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void ZiDianBianJi_Load(object sender, EventArgs e)
        {
            BindFenZu();
            if (m_FocusedRow != null)
            {
                cmb_fenzu.Text = string.Format("{0}", m_FocusedRow.Cells["col_fenzu"].Value);
                txt_xiangmumingcheng.Text = string.Format("{0}", m_FocusedRow.Cells["col_xiangmu"].Value);
            }
        }

        private void BindFenZu()
        {
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", "fenzu", null, "", "fenzu", null);
            cmb_fenzu.DisplayMember = "fenzu";
            cmb_fenzu.ValueMember = "fenzu";
            cmb_fenzu.DataSource = Dt;
        }
    }
}
